package com.erpanderp.wam.usm.model.service.user;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.tm.model.entity.RRFOfferLetterHRInputs;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;
import com.erpanderp.wam.util.WAMPasswordUtil;
import com.sun.org.apache.bcel.internal.generic.LSTORE;

public class WAMUserDAO {

	public WAMUser validateWAMUser(String employeeCode, String password) {
		WAMUser wamUser = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from WAMUser w where w.employeeCode = :employeeCode and w.password = :password and w.employee.status =:status";
			Query query = session.createQuery(queryString)
					.setParameter("employeeCode", employeeCode)
					.setParameter("password", WAMPasswordUtil.encrypt(password)).setString("status", CommonLookup.STATUS_ACTIVE);
			query.setCacheable(false);
			List temp = query.list();

			if (temp != null && temp.size() > 0) {
				wamUser = (WAMUser) temp.get(0);
			}

			session.getTransaction().commit();
			return wamUser;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public WAMUser wamUserExistense(String employeeCode, String password) {
		WAMUser wamUser = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from WAMUser w where w.employeeCode = :employeeCode and w.password = :password";
			Query query = session.createQuery(queryString)
					.setParameter("employeeCode", employeeCode)
					.setParameter("password", WAMPasswordUtil.encrypt(password));
			query.setCacheable(false);
			List temp = query.list();

			if (temp != null && temp.size() > 0) {
				wamUser = (WAMUser) temp.get(0);
			}

			session.getTransaction().commit();
			return wamUser;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<WAMUser> getWAMUsers() {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from WAMUser";
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			List<WAMUser> wamUsers = query.list();
			session.getTransaction().commit();
			return wamUsers;		
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Employee getEmployeeByEmail(String email) {
		Employee employee = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String querString = "from Employee e where e.email=:email";
			Query query = session.createQuery(querString).setString("email",
					email);
			// employee = (Employee) query.uniqueResult();
			System.out.println("EMPL OBJ IN DAO ========" + employee);
			query.setCacheable(false);
			List temp = query.list();

			if (temp != null && temp.size() > 0) {
				employee = (Employee) temp.get(0);
			}

			session.getTransaction().commit();
			return employee;

		} catch (Exception e) {
			e.printStackTrace();
			return null;

		}
	}

	public WAMUser getPasswordByEmployeeId(String employeeCode) {
		WAMUser wamUser = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String querString = "from WAMUser e where e.employeeCode=:employeeCode";
			Query query = session.createQuery(querString).setString(
					"employeeCode", employeeCode);
			wamUser = (WAMUser) query.uniqueResult();

			session.getTransaction().commit();
			return wamUser;

		} catch (Exception e) {
			e.printStackTrace();
			return null;

		}
	}

	public boolean isEmailRegistered(String email) {
		boolean EMAIL_REGISTERED = false;
		Employee employee = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from Employee e where e.email=:email";
			Query query = session.createQuery(queryString).setString("email",
					email);
			employee = (Employee) query.uniqueResult();
			if (employee != null) {
				EMAIL_REGISTERED = true;
			} else {
				EMAIL_REGISTERED = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
			// TODO: handle exception
		}
		return EMAIL_REGISTERED;
	}

	public WAMUser getWAMUserByEmployeeId(Long employeeId) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from WAMUser wu where wu.employee.employeeId = :employeeId";
			Query query = session.createQuery(queryString).setLong("employeeId",	employeeId);
			List<WAMUser> temp  = (List<WAMUser>) query.list();
			if(temp != null && temp.size() > 0){
				return temp.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		return null;
	}
	
	public WAMUser updateWAMUserPassword(WAMUser wamUser) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			session.update(wamUser);
			session.getTransaction().commit();
			return wamUser;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean updateWAMUsers(List<WAMUser> wamUsers) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			if(wamUsers !=null && wamUsers.size() > 0){
				for(WAMUser wamUser : wamUsers){
					session.update(wamUser);
				}			
			}
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	
	public static void main(String a[]){
		System.out.println(new WAMUserDAO().validateWAMUser("E50051", "erp123"));
		
	}
}
